//
//  TribeNewViewController.m
//  yigou
//
//  Created by SunChao on 17/3/6.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "TribeNewViewController.h"
#import "TitleView.h"
#import "SearchViewController.h"
#import "TribeLeftViewController.h"
#import "TribeRightViewController.h"
#import "MsgCenterViewController.h"
#define TopViewHeight 50
#define slideViewFrame1 CGRectMake(0, TopViewHeight-2, SCREEN_WIDTH/2, 2)
#define slideViewFrame2 CGRectMake(SCREEN_WIDTH/2, TopViewHeight-2, SCREEN_WIDTH/2, 2)
@interface TribeNewViewController ()<UISearchBarDelegate,UIScrollViewDelegate>

@property (nonatomic,strong)TitleView *titleView;
@property (nonatomic,strong )UIScrollView *scrollView;

@property (nonatomic,strong) UIView *topView;

@property (strong, nonatomic) UIView *slideView;

@property (strong,nonatomic) TribeLeftViewController *leftVC;

@property (strong,nonatomic) TribeRightViewController *rightVC;

@property (strong, nonatomic) UIButton *noticebtn;
@property (strong, nonatomic) UILabel *noticecount;

@end

@implementation TribeNewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNav];
    [self initTopTabs];
    [self initScrollView];
    [self initDownTables];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess:) name:POST_LOGINSUC object:nil];
}

- (void) loginSuccess:(NSNotification *) notify {
    [_leftVC refresh];
    [_rightVC refresh];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [ViewFactory setNavigationBlack:self];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}
#pragma -mark 设置导航栏
- (void)setupNav{
    [ViewFactory setNavigationBlack:self];
    self.navigationItem.titleView = [ViewFactory makeNavigationTitle:@"部落"];
    
    self.titleView = [[TitleView alloc] initWithFrame:CGRectMake(12, 0, ScreenWidth - 24, 44)];
    self.titleView.backgroundColor = [UIColor clearColor];
    UISearchBar *searchbar = [[UISearchBar alloc]initWithFrame:CGRectMake(12, 7, ScreenWidth - 24, 30)];
    searchbar.searchBarStyle = UISearchBarStyleDefault;
    searchbar.placeholder = @"搜索你喜欢的部落";
    searchbar.layer.masksToBounds = YES;
    searchbar.layer.cornerRadius = 4.f;
    searchbar.delegate = self;
    [self.titleView addSubview:searchbar];
    
//    self.noticebtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.noticebtn.frame = CGRectMake(ScreenWidth - 40, 12, 20, 20);
//    [self.noticebtn setBackgroundImage:[UIImage imageNamed:@"topnotice.png"] forState:UIControlStateNormal];
//    [self.noticebtn addTarget:self action:@selector(clickNotice:) forControlEvents:UIControlEventTouchUpInside];
//    
//    UIButton *clickbtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    clickbtn.frame = CGRectMake(ScreenWidth - 50, 0, 40, 44);
//    [self.titleView addSubview:clickbtn];
//    [clickbtn addTarget:self action:@selector(clickNotice:) forControlEvents:UIControlEventTouchUpInside];
//    
//    self.noticecount = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 10, 10)];
//    self.noticecount.font = [FontTool fontWithSize:10];
//    self.noticecount.textAlignment = NSTextAlignmentCenter;
//    self.noticecount.textColor = [UIColor whiteColor];
//    self.noticecount.backgroundColor = [UIColor colorWithHexString:@"0EA1B6"];
//    self.noticecount.layer.masksToBounds = YES;
//    self.noticecount.layer.cornerRadius = 5.f;
//    self.noticecount.text = @"1";
//    self.noticecount.userInteractionEnabled = NO;
//    self.noticecount.hidden = YES;
//    [self.noticebtn addSubview:self.noticecount];
//    [self.titleView addSubview:self.noticebtn];
    
    self.navigationItem.titleView = self.titleView;
}

- (void)clickNotice:(id)sender {
    MsgCenterViewController *vc = [[MsgCenterViewController alloc] initWithNibName:@"MsgCenterViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)getMsgCount {
    DYBaseRequest *request = [DYBaseRequest new];
    request.requestUrl = [NSString stringWithFormat:@"%@/sys/index/message/count", APIYIGOU];
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *dict = [NSDictionary changeType:request.responseJSONObject];
        NSInteger count = [[dict objectForKey:@"data"] integerValue];
        self.noticecount.text = [NSString stringWithFormat:@"%ld", count];
        if (count == 0) {
            self.noticecount.hidden = YES;
        } else {
            self.noticecount.hidden = NO;
        }
    } error:^(YTKBaseRequest *request) {
        
    } failure:^(YTKBaseRequest *request) {
    }];
}

#pragma mark -- 实例化顶部的tab
-(void) initTopTabs{
    
    _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, TopViewHeight)];
    CGFloat width = SCREEN_WIDTH / 2;
    NSArray *titleArr = @[@"推荐",@"我的"];
    for (int i = 0; i < 2; i ++) {
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * width, 0, width, TopViewHeight)];
        
        view.backgroundColor = [UIColor whiteColor];
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, TopViewHeight)];
        button.tag = i+10;
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitle:[titleArr objectAtIndex:i] forState:UIControlStateNormal];
        [button setFont:[UIFont systemFontOfSize:16]];
        [button addTarget:self action:@selector(tabButton:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
        [_topView addSubview:view];
    }
    
    _slideView = [[UIView alloc] initWithFrame:slideViewFrame1];
    [_slideView setBackgroundColor:QINGColor];
    [_topView addSubview:_slideView];
    [self.view addSubview:_topView];
}

#pragma mark -- 实例化ScrollView
-(void) initScrollView{
    
    CGFloat y = CGRectGetMaxY(_topView.frame);
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, y, SCREEN_WIDTH, SCREEN_HEIGHT-64-TopViewHeight-44)];
    
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 2, SCREEN_HEIGHT-64-TopViewHeight-44);
    
    _scrollView.pagingEnabled = YES;
    
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
}

#pragma mark --点击顶部的按钮所触发的方法
-(void) tabButton: (id) sender{
    
    UIButton *button = sender;
    _slideView.frame = CGRectMake((button.tag-10)*(SCREEN_WIDTH/2), TopViewHeight-2, SCREEN_WIDTH/2, 2);
    [_scrollView setContentOffset:CGPointMake((button.tag-10) * SCREEN_WIDTH, 0) animated:YES];
    NSLog(@"%lf,%ld",_scrollView.contentOffset.x,(long)button.tag);
    [[NSNotificationCenter defaultCenter]postNotificationName:@"tabButtonClicked" object:nil];
}

#pragma mark --初始化下方的TableViews
-(void) initDownTables{
    
    _leftVC = [[TribeLeftViewController alloc]init];
    [self addChildViewController:_leftVC];
    _leftVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-TopViewHeight-64-40);
    [self.scrollView addSubview:_leftVC.view];
    
    _rightVC = [[TribeRightViewController alloc]init];
    [self addChildViewController:_rightVC];
    [self.scrollView addSubview:_rightVC.view];
    _rightVC.view.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT-TopViewHeight-64-40);
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x == 0) {
        self.slideView.frame = slideViewFrame1;
    }else if (scrollView.contentOffset.x == SCREEN_WIDTH){
        self.slideView.frame = slideViewFrame2;
    }
}

#pragma -mark 搜索
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    SearchViewController *vc = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
    return NO;
}


@end
